package com.example.radiationarea.block;

import com.example.radiationarea.effect.ModEffects;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.effect.MobEffectInstance;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ContaminatedDirtBlock extends Block {
    private static final Logger LOGGER = LogManager.getLogger();
    
    public ContaminatedDirtBlock(Properties properties) {
        super(properties);
    }

    @Override
    public void stepOn(Level level, BlockPos pos, BlockState state, Entity entity) {
        // Trigger when entity steps on this block
        if (!level.isClientSide && entity instanceof LivingEntity) {
            // Only execute on server side
            LivingEntity livingEntity = (LivingEntity) entity;
            
            // Add radiation effect to entity, lasting 600 ticks (30 seconds)
            livingEntity.addEffect(new MobEffectInstance(ModEffects.RADIATION.get(), 600, 0));
            
            LOGGER.debug("Entity {} stepped on contaminated dirt at position {} and got radiation effect", entity.getName().getString(), pos);
        }
        super.stepOn(level, pos, state, entity);
    }
    
    @Override
    public void entityInside(BlockState state, Level level, BlockPos pos, Entity entity) {
        // Continuously apply effect while entity is inside/on the block
        if (!level.isClientSide && entity instanceof LivingEntity) {
            LivingEntity livingEntity = (LivingEntity) entity;
            
            // Keep the radiation effect active while on the block
            MobEffectInstance effect = livingEntity.getEffect(ModEffects.RADIATION.get());
            if (effect == null || effect.getDuration() <= 10) {
                livingEntity.addEffect(new MobEffectInstance(ModEffects.RADIATION.get(), 600, 0));
                LOGGER.debug("Entity {} refreshed radiation effect on contaminated dirt at position {}", entity.getName().getString(), pos);
            }
        }
        super.entityInside(state, level, pos, entity);
    }
}